package kr.saeildamil.androidstudy.ch12;

import java.util.ArrayList;

import kr.saeildamil.androidstudy.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;

public class MyListAdapter extends BaseAdapter {
	Context mainContext;
	int icontextLayout;
	ArrayList<MyitemEx> arSrc;
	LayoutInflater Inflater;

	public MyListAdapter(Context mainContext, int icontextLayout,
			ArrayList<MyitemEx> arSrc) {
		super();
		this.mainContext = mainContext;
		this.icontextLayout = icontextLayout;
		this.arSrc = arSrc;
		this.Inflater = (LayoutInflater) mainContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		return arSrc.size();
	}

	public Object getItem(int position) {
		return arSrc.get(position).name;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		final int pos = position;
		if (convertView == null) {
			convertView = Inflater.inflate(icontextLayout, parent, false);
		}
		ImageView img = (ImageView) convertView
				.findViewById(R.id.ch12_icontext_iv);
		img.setImageResource(arSrc.get(position).iCon);

		TextView tv = (TextView) convertView
				.findViewById(R.id.ch12_icontext_tv);
		tv.setText(arSrc.get(position).name);

		TextView message = (TextView) convertView
				.findViewById(R.id.ch12_icontext_message);
		message.setText(arSrc.get(position).message);

		TextView btn = (TextView) convertView
				.findViewById(R.id.ch12_icontext_btn);
		final LinearLayout detailLayout = (LinearLayout) convertView
				.findViewById(R.id.ch12_icontext_detaillayout);

		btn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String str = arSrc.get(pos).name + "주문합니다.";
				Toast.makeText(mainContext, str, Toast.LENGTH_LONG).show();
				LinearLayout.LayoutParams vparam = (LayoutParams) detailLayout.getLayoutParams();

				if (vparam.weight == 0) {
					vparam.weight = 1;
				} else {
					vparam.weight = 0;
				}
				detailLayout.setLayoutParams(vparam);
			}
		});

		return convertView;
	}

}
